package com.cykj.pos.controller.task;

import com.alibaba.fastjson.JSONObject;
import com.cykj.pos.domain.BizMerchantInfo;
import com.cykj.pos.domain.BizOrganization;
import com.cykj.pos.mapper.BizMerchantInfoMapper;
import com.cykj.pos.mapper.BizMerchantMapper;
import com.cykj.pos.service.IBizMerchantInfoService;
import com.cykj.pos.service.IBizMerchantService;
import com.google.gson.Gson;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TaskBackMerchant {
    @Autowired
    IBizMerchantInfoService bizMerchantInfoService;
    @Autowired
    IBizMerchantService bizMerchantService;
    @Resource
    private BizMerchantInfoMapper bizMerchantInfoMapper;

//    @Scheduled(fixedDelay = 500)
    public void backMerchantInfo(){
        List<String> list=bizMerchantInfoMapper.selectBackMerchant();
        for (String merchantCode:list){
            BizMerchantInfo bizMerchantInfo=new BizMerchantInfo();
            bizMerchantInfo.setMerchantCode(merchantCode);
            BizMerchantInfo bizMerchantInfo1=bizMerchantInfoService.getMerchantInfo(bizMerchantInfo);
            List<BizOrganization> listOrgan = bizMerchantService.selectBizOrganization("POSPBJ");
            Map<String,String> data=new HashMap<String,String>();
            data.put("uuid",bizMerchantInfo1.getUserName());
            data.put("merchantCode",bizMerchantInfo1.getMerchantCode());
            data.put("merchantId",bizMerchantInfo1.getMerchantId());
            data.put("merchantKey",bizMerchantInfo1.getMerchantKey());
            data.put("masterKeys",bizMerchantInfo1.getMasterKeys());
            data.put("validationValue",bizMerchantInfo1.getValidationValue());
            data.put("merchantLevel",bizMerchantInfo1.getMerchantLevel());
            data.put("auditStatus",bizMerchantInfo1.getAuditStatus());
            data.put("merchantDesc",bizMerchantInfo1.getMerchantDesc());
            for (BizOrganization organ:listOrgan){
                if(organ.getOrgncode().equals(bizMerchantInfo1.getMerchId().toString())){
                    String orgip = organ.getOrgip();
                    String orgport = organ.getOrgport();
                    String url = orgip + orgport;

                    System.out.println(url+"     "+data.toString());
                    String result = doPost(url, new Gson().toJson(data));
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    String ret_code = jsonObject.getString("ret_code");
                    if (ret_code.equals("0")){
                        System.out.println("--------------");
                        System.out.println("商戶信息转发到---->" + organ.getOrgnname());
                        System.out.println("--------------");
                    }
                }
            }
            bizMerchantInfoMapper.deleteBackMerchant(merchantCode);
        }
    }

    public String doPost(String url,String data){
        //创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try{
            //创建http请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
            //创建请求内容
            String jsonStr = data;
            StringEntity entity = new StringEntity(jsonStr,"utf-8");
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(),"utf-8");
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            if(response != null){
                try {
                    response.close();
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
            if(httpClient != null){
                try{
                    httpClient.close();
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
        }
        return result;
    }

}
